/* Copyright (c) 2009 Mark L. Howison
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  (1) Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *  (2) Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *  (3) The name of the copyright holder may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package edu.berkeley.edrl.wiikinemathics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;
import wiiremotej.IRLight;

/**
 *
 * @author mhowison
 */
public class CameraPanel extends JPanel {

    private int[] x = new int[WiiKinemathicsApp.WIICAM_SENSORS];
    private int[] y = new int[WiiKinemathicsApp.WIICAM_SENSORS];
    private int[] r = new int[WiiKinemathicsApp.WIICAM_SENSORS];

    public CameraPanel() {
        setBackground(Color.BLACK);
        for (int i=0; i<WiiKinemathicsApp.WIICAM_SENSORS; i++) {
            x[i] = -1;
            y[i] = -1;
            r[i] = 0;
        }
    }

    public void update(IRLight[] lights) {
        for (int i=0; i<lights.length; i++) {
            if (lights[i] != null) {
                x[i] = (int)(lights[i].getX()*getWidth());
                y[i] = (int)((1.0 - lights[i].getY())*getHeight());
                r[i] = (int)(lights[i].getSize()*getHeight()*0.25);
                if (r[i] < 5) r[i] = 5;
                if (WiiKinemathicsApp.DEBUG) {
                    WiiKinemathicsApp.getApplication().getLogger().info(
                            String.format("camera sensor %d: %d %d %d", i, x[i], y[i], r[i]));
                }
            } else {
                x[i] = -1;
                y[i] = -1;
                r[i] = 0;
            }
        }
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(Color.WHITE);
        g2.setStroke(new BasicStroke(2.F));

        for (int i=0; i<WiiKinemathicsApp.WIICAM_SENSORS; i++) {
            if (x[i] >= 0 && y[i] >= 0) {
                g2.fillOval(x[i]+r[i], y[i]+r[i], 2*r[i], 2*r[i]);
            }
        }
    }

}
